//
//  WlanChecker.m
//  gdemm
//
//  Created by 乔明超 on 16/10/28.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "WlanChecker.h"
#import "WIFIManager.h"
#import "DeviceInfo.h"
#import "WifiInfo.h"
#import "StringUtil.h"
#import "ExamineManager.h"
#define NAME_TAG @"name"
#define SSID_TAG @"ssid"
#define IP_TAG @"ip"
#define MAC_TAG @"mac"
@interface WLANChecker()
//黑名单模式
@property(assign,nonatomic) BOOL mBlackMode;

//需要检测的WIFI列表
@property(strong,nonatomic) NSMutableArray *mCheckList;
@property(strong,nonatomic) NSMutableArray *mInfoList;

//是否合法
@property(assign,nonatomic) BOOL mIslegal;
@end
@implementation WLANChecker
static WLANChecker *mInstance;

+ (instancetype)getInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mInstance = [[self alloc]init];
    });
    return mInstance;
    
}

- (NSString *)getExamineDesc{

    return @"网络环境检测";
}

- (NSInteger)getRepairType{
    return RT_MANUAL;
}

- (BOOL)run{
    
    NSString *SSID = [WIFIManager getWIFISSID];
    NSString *ssid = [SSID lowercaseString];

    if (ssid == nil) {
        if (self.mActionListener != nil) {
            [self reportExamineInfo:@"WlanCheck&正在测评...%@&0"];
            [NSThread sleepForTimeInterval:0.2];
            NSString *text = [NSString stringWithFormat:@"当前没有连接网络"];
            [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&%@&1",text]];
            [[ExamineManager getInstance].examineResult setObject:@1 forKey:EXAMINE_WLAN_STATUS_TAG];
        }
        return YES;
    }


    NSString *ip = [DeviceInfo getIpAddress];
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"com.apple.configuration.managed"];
    NSString *mac = dict[@"mac"];
    if (self.mInfoList.count == 0) {
        _mIslegal = YES;
        [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&正在测评...%@&0",SSID]];
        [NSThread sleepForTimeInterval:0.2];
        NSString *text = [NSString stringWithFormat:@"当前wifi[%@]网络环境安全,允许连接",SSID];
        [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&%@&1",text]];
        //测评安全
        [[ExamineManager getInstance].examineResult setObject:@1 forKey:EXAMINE_WLAN_STATUS_TAG];
        return YES;
    }
    
    HRLog(@"ssid is : %@",ssid);
    HRLog(@"Ip is : %@",ip);
    HRLog(@"MCheckList is : %@",self.mCheckList);
    
    for (WifiInfo *info in self.mInfoList) {
        BOOL isMacContains = NO;
        BOOL isIpContains = NO;
        BOOL isSSIDCOntains = NO;
        
        NSString *tmpIp = info.ipAddress;
        NSString *tmpMac = info.macAddress;
        NSString *tmpSSID = [info.ssid lowercaseString];
        HRLog(@"ssid = %@ current = %@",tmpSSID,ssid);
        [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&正在测评...%@&0",tmpSSID]];
        [NSThread sleepForTimeInterval:0.2];
        if (![StringUtil isEmpty:tmpSSID] && [tmpSSID isEqualToString:ssid]) {
            isSSIDCOntains = YES;
        }else if ([StringUtil isEmpty:tmpSSID]){
            isSSIDCOntains = YES;
        }
        
        if (![StringUtil isEmpty:tmpMac] && [tmpMac isEqualToString:mac]) {
            isMacContains = YES;
        }else if ([StringUtil isEmpty:tmpMac]){
            isMacContains = YES;
        }
        
        if (![StringUtil isEmpty:tmpIp] && [tmpIp isEqualToString:ip]) {
            isIpContains = YES;
        }else if ([StringUtil isEmpty:tmpIp]){
            isIpContains = YES;
        }
        
        //如果黑名单模式
        if (_mBlackMode) {
            if (isIpContains && isMacContains && isSSIDCOntains) {
                _mIslegal = NO;
                break;
            }else{
                _mIslegal = YES;
            }
        }else{
            if (isIpContains && isMacContains && isSSIDCOntains) {
                _mIslegal = YES;
                 break;
            }else{
                _mIslegal = NO;
               
            }
        }
    }
    NSString *text;
    if (!_mIslegal) {
        //测评不安全
        [[ExamineManager getInstance].examineResult setObject:@"wlanUnsafe" forKey:EXAMINE_WLAN_STATUS_TAG];
        
        if (_mBlackMode) {
            text = [NSString stringWithFormat:@"当前为黑名单模式,不允许连接此wifi[%@]",SSID];
            [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&%@&0",text]];
        }else{
            text = [NSString stringWithFormat:@"当前为白名单模式,不允许连接此wifi[%@]",SSID];
            [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&%@&0",text]];
        }
        
    }else{
        //测评安全
        [[ExamineManager getInstance].examineResult setObject:@1 forKey:EXAMINE_WLAN_STATUS_TAG];
        text = [NSString stringWithFormat:@"当前wifi[%@]网络环境安全,允许连接",SSID];
        [self reportExamineInfo:[NSString stringWithFormat:@"WlanCheck&%@&1",text]];
    }
    
    return _mIslegal;
}

- (id)details{

    return self.mCheckList;
}

//修复WIFI危险项
- (BOOL)repair{
    return NO;
}

//获取WALN检测规则数据
- (void)getExamineContent:(NSDictionary *)content{
    
    [self.mCheckList removeAllObjects];
    [self.mInfoList removeAllObjects];
    NSString *mode = content[CHECK_APP_OR_WLAN_MODE];
    if ([mode isEqualToString:CHECK_APP_OR_WLAN_MODE_BLACK]) {
        _mBlackMode = YES;
    }else{
        _mBlackMode = NO;
    }
    NSString *key = _mBlackMode?CHECK_APP_OR_WLAN_MODE_BLACKTAG : CHECK_APP_OR_WLAN_MODE_WHITETAG;
    
    self.mInfoList = [WifiInfo wifiInfoWithArray:content[key]];
    for (WifiInfo *info in self.mInfoList) {
        HRLog(@"name = %@",info.name);
        HRLog(@"ssid = %@",info.ssid);
        HRLog(@"ipAddress = %@",info.ipAddress);
        HRLog(@"macAddress = %@",info.macAddress);
        HRLog(@"lowercaseString = %@",[info.ssid lowercaseString]);
        [self.mCheckList addObject:[info.ssid lowercaseString]];
    }
}


/**
 *  判断是否连接WIFI
 *  @return 连接WIFI,返回true,未连接返回false;
 */

- (BOOL)isWlanConnected{

    return NO;
}

- (BOOL)returnMode{

    return _mBlackMode;
}

- (NSInteger)getExamineContentCount{

    return self.mCheckList.count;
}

- (BOOL)returnValue{
    return _mIslegal;
}

- (void)reset{

    [self.mCheckList removeAllObjects];
    [self.mInfoList removeAllObjects];
}






@end
